package com.yunzong.iot.http;

import android.text.TextUtils;
import android.util.Log;

import com.com.yunzong.rainbowbridge.network.LogInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.yunzong.iot.config.IotConfig.AUTHURL;

/**
 * 为了多活，灵活控制 baseUrl
 * @author 克林
 * 通过url 生产retrofit,不改变okhttpclient
 */
public class RequestClient {

    private static final int DEFAULT_TIMEOUT = 15;

    /**
     * okhttp,httpclient
     */
    private static OkHttpClient okHttpClient;

    private static Retrofit retrofit;

    private static RequestClient retrofitClient;





    public static RequestClient getInstance(){
        if(retrofitClient==null){
            synchronized (RequestClient.class){
                if(retrofitClient==null){
                    retrofitClient=new RequestClient();
                }
            }
        }
        return retrofitClient;
    }

    /**
     * 当url 发生变化重新构建retrofit
     * 比如：多活url切换
     * @param url
     * @return
     */
    public   Retrofit buildRetrofit(String url){
        if (TextUtils.isEmpty(url)) {

            url = AUTHURL;
        }
        Log.e("url","url="+url);
        retrofit=new Retrofit.Builder()
                .client(getOkHttpClient())
                .baseUrl(AUTHURL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return  retrofit;
    }

    /**
     * 初始化okhttpclient
     * @return
     */
    private  OkHttpClient getOkHttpClient(){
        if(okHttpClient==null){
            synchronized (RequestClient.class){
                if(okHttpClient==null){
                    okHttpClient=new OkHttpClient.Builder()
                            //.addInterceptor(new CallChainInterceptor())
//                            .hostnameVerifier(new TrustAllHostnameVerifier())

                            .addInterceptor( new LogInterceptor())
                            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                            .readTimeout(10_000, TimeUnit.MILLISECONDS)
                            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                            .connectionPool(new ConnectionPool(HttpConfig.MAX_IDLE_CONNECTIONS,
//                                    HttpConfig.KEEP_ALIVE_DURATION,TimeUnit.SECONDS))
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    /**
     * 交给代理对象去处理,会频繁创建动态代理
     * @param service
     * @param <T>
     * @return
     */
    public <T> T create(final Class<T> service) {
        if(service==null){
            throw new RuntimeException("Api service is null!");
        }
        return retrofit.create(service);
    }


}
